/**@FILE:UserAction.java
 * @AUTHOR:qingsheng.zheng
 * @DATE:2015-2-1 上午11:41:47*/
package com.xf.toy.web;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.xf.pub.exception.ServiceException;
import com.xf.pub.logger.LoggerUtil;
import com.xf.pub.tool.StringUtils;
import com.xf.toy.bean.User;
import com.xf.toy.service.UserService;
import com.xf.toy.web.common.BaseAction;
import com.xf.toy.web.framework.annotation.Operlog;

/*******************************************
 * @COMPANY:alibaba
 * @CLASS:UserAction
 * @DESCRIPTION:
 * @AUTHOR:qingsheng.zheng
 * @VERSION:v1.0
 * @DATE:2015-2-1 上午11:41:47
 *******************************************/
@Controller
@RequestMapping(value = "/toy/user")
public class UserAction extends BaseAction {
	@Resource(name = "userService")
	private UserService userService;

	@Operlog
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public void userinfo(@RequestParam(required = false) Long userid,
			@RequestParam(required = false) String openid,
			HttpServletRequest req, HttpServletResponse resp) {
		try {
			User user = null;
			if (userid != null && userid != 0)
				user = userService.selectUserDetail(userid);
			else if (!StringUtils.isNull(openid))
				user = userService.selectUserByOpenId(openid);
			else
				user = null;
			if (user != null) {
				req.getSession().setAttribute("userid", user.getUserId());
				operLogger.info(LoggerUtil.logstr("method", "toy/user/detail",
						"userid", userid + ""));
			}
			response(resp, user);
		} catch (ServiceException e) {
			response(resp, e);
		}
	}

	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public void registUser(@RequestParam String openid,
			@RequestParam(required = false, defaultValue = "") String address,
			@RequestParam(required = false, defaultValue = "") String invitecode,
			@RequestParam(required = false, defaultValue = "") String mobile, 
			HttpServletRequest req,
			HttpServletResponse resp) {
		try {
			User user = new User();
			user.setAddress(address);
			user.setMobile(mobile);
			user.setCreatetime(new Date());
			user.setMoneyRemain(0D);
			user.setOpenidWx(openid);
			user.setUsername("");
			operLogger.info(LoggerUtil
					.logstr("method", "toy/info/register", "openid", openid
							+ "", "mobile", mobile, "address", address));
			userService.registUserWithInviteCode(user, invitecode);
			response(resp, user);
		} catch (ServiceException e) {
			response(resp, e);
		}
	}

	@RequestMapping(value = "/updateuser", method = RequestMethod.GET)
	public void updateUser(@RequestParam Long userid,
			@RequestParam String address, @RequestParam String mobile,
			HttpServletRequest req, HttpServletResponse resp) {
		try {
			operLogger.info(LoggerUtil
					.logstr("method", "toy/user/updateuser", "userid", userid
							+ "", "address", address, "mobile", mobile));
			User user = new User();
			user.setUserId(userid);
			user.setAddress(address);
			user.setMobile(mobile);
			userService.updateUser(user);
			response(resp, null);
		} catch (ServiceException e) {
			response(resp, e);
		}
	}

}
